/**
 * ${tableComment} Actions
 *
 * @author motorfu
 * @email ffu@maxleap.com
 * @since ${date}
 */
import { LoadingCommon, ResponseCallback } from "../../common-module/actions/BaseAction";
import { PostResource, DeleteResource, GetResource, PutResource } from "../../utils/fetch";

export const ${tableName?upper_case}_PAGE_LOADING = "${tableName?lower_case}_page_loading";

export const ${tableName?upper_case}_PAGE_DATA = "${tableName?lower_case}_page_data";

export const ${tableName?upper_case}_BATCH_REMOVE_LOADING = "${tableName?lower_case}_batch_remove_loading";

export const ${tableName?upper_case}_REMOVE_LOADING = "${tableName?lower_case}_remove_loading";

export const ${tableName?upper_case}_CREATE_LOADING = "${tableName?lower_case}_create_loading";

export const ${tableName?upper_case}_MODIFY_LOADING = "${tableName?lower_case}_modify_loading";

const BASE_PATH = "/${path}";

/**
 * 创建${tableComment}
 * @param params
 * @param callback
 * @returns {Function}
 */
export const ${className}CreateFunc = (params, callback) => {
  return (dispatch) => {
    LoadingCommon(dispatch, ${tableName?upper_case}_CREATE_LOADING);
    PostResource(`${r"${BASE_PATH}"}/create`, params).then(response => {
      LoadingCommon(dispatch, ${tableName?upper_case}_CREATE_LOADING, false);
      ResponseCallback(callback, response, null, true);
    });
  };
}

/**
 * 删除${tableComment}
 * @param id
 * @param callback
 * @returns {Function}
 */
export const ${className}RemoveFunc = (id, callback) => {
  return (dispatch) => {
    LoadingCommon(dispatch, ${tableName?upper_case}_REMOVE_LOADING);
    DeleteResource(`${r"${BASE_PATH}"}/${r"${id}"}`).then(response => {
      LoadingCommon(dispatch, ${tableName?upper_case}_REMOVE_LOADING, false);
      ResponseCallback(callback, response, null, true);
    });
  };
}

/**
 * 批量删除${tableComment}
 * @param ids
 * @param callback
 * @returns {Function}
 */
export const ${className}BatchRemoveFunc = (ids, callback) => {
  return (dispatch) => {
    LoadingCommon(dispatch, ${tableName?upper_case}_BATCH_REMOVE_LOADING);
    DeleteResource(`${r"${BASE_PATH}"}`, ids).then(response => {
      LoadingCommon(dispatch, ${tableName?upper_case}_BATCH_REMOVE_LOADING, false);
      ResponseCallback(callback, response, null, true);
    });
  };
}

/**
 * 更新${tableComment}
 * @param params
 * @param callback
 * @returns {Function}
 */
export const ${className}ModifyFunc = (params, callback) => {
  return (dispatch) => {
    LoadingCommon(dispatch, ${tableName?upper_case}_MODIFY_LOADING);
    PutResource(`${r"${BASE_PATH}"}/modify`, params).then(response => {
      LoadingCommon(dispatch, ${tableName?upper_case}_MODIFY_LOADING, false);
      ResponseCallback(callback, response, null, true);
    });
  };
}

const ${className}PageData = (params, response) => {
  const data = {
    params: params,
    data: response.data
  };
  return {
    type: ${tableName?upper_case}_PAGE_DATA,
    data: data
  };
};

/**
 * 获取账号列表
 * @param params
 * @param callback
 * @returns {Function}
 */
export function ${className}PageFunc(params = {pageNum: 1, pageSize: 15}, callback) {
  return (dispatch) => {
    LoadingCommon(dispatch, ${tableName?upper_case}_PAGE_LOADING);
    PostResource(`${r"${BASE_PATH}"}/search`, params).then(response => {
      if (response.code !== 200){
        LoadingCommon(dispatch, ${tableName?upper_case}_PAGE_LOADING, false);
      }
      ResponseCallback(callback, response, () => dispatch(${className}PageData(params, response)), true);
    });
  };
}
